package com.geyue.controller;

import com.geyue.common.toolbox.result.Res;
import com.geyue.core.model.Admin;
import com.geyue.core.model.Teacher;
import com.geyue.core.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by Lyon on 2017/4/24.
 */
@Controller
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @RequestMapping(value = "/",method = RequestMethod.GET)
    public String index(HttpSession session){
        Admin admin = (Admin) session.getAttribute("admin");

        if(admin == null)
            return "error";

        return "teacher/list";
    }

    @RequestMapping(value = "/page", method = RequestMethod.POST)
    @ResponseBody
    public Res index(Teacher teacher, Integer pageSize, Integer pageNum, HttpSession session) {
        Admin admin = (Admin) session.getAttribute("admin");

        if(admin == null)
            return Res.fail("登录信息过时,请重新登录!");

        return Res.success("", teacherService.getPage(pageNum, pageSize, teacher));
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public String index(@PathVariable Integer id, HttpSession session, Model model){
        Admin admin = (Admin) session.getAttribute("admin");

        if(admin == null)
            return "error";

        model.addAttribute("info",teacherService.getOneByKey(id));
        return "teacher/info";
    }

    @RequestMapping(value = "/pay",method = RequestMethod.GET)
    public String pay(HttpSession session, Model model){
        Admin admin = (Admin) session.getAttribute("admin");

        if(admin == null)
            return "error";

        return "teacher/pay";
    }

    @RequestMapping(value = "/pay",method = RequestMethod.PUT)
    @ResponseBody
    public Res pay(Integer id,Integer money, HttpSession session, Model model){
        Admin admin = (Admin) session.getAttribute("admin");

        if(admin == null)
            return Res.fail("登录信息过期,请重新登录!");

        return teacherService.pay(id,money);
    }
}
